package com.zzyl.serve.service.impl.admin;

import java.util.List;
import java.util.Arrays;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.zzyl.common.utils.bean.BeanUtils;
import com.zzyl.serve.domain.admin.NursingProjectPlan;
import com.zzyl.serve.dto.NursingPlanDto;
import com.zzyl.serve.mapper.admin.NursingProjectPlanMapper;
import com.zzyl.serve.service.admin.INursingProjectPlanService;
import com.zzyl.serve.vo.NursingPlanVo;
import com.zzyl.serve.vo.NursingProjectPlanVo;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.admin.NursingPlanMapper;
import com.zzyl.serve.domain.admin.NursingPlan;
import com.zzyl.serve.service.admin.INursingPlanService;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

/**
 * 护理计划Service业务层处理
 *
 * @author itheima
 * @date 2025-03-03
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    /*建一个常量*/
    private static final String CACHE_NURSINGPLAN_KEY = "nursingPlan:all";
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;
    @Autowired
    private INursingProjectPlanService nursingProjectPlanService;

    /**
     * 查询护理计划
     *
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Integer id) {
        // 1. 根据id查询护理计划
        NursingPlan plan = getById(id);

        // 2. 属性拷贝 nursingPlan到 nursingPlanVo中
        // TODO:  BeanUtils的拷贝必须 属性名 + 类型都一致才能拷贝。
        NursingPlanVo vo = new NursingPlanVo();
        BeanUtils.copyProperties(plan, vo);

        // 3. 根据护理计划id查询关联的所有护理项目计划表的list
        List<NursingProjectPlanVo> projectPlans = nursingProjectPlanMapper.selectByPlanId(id);

        // 4. 将list赋值给nursingPlanVo
        vo.setProjectPlans(projectPlans);

        // 5. 返回nursingPlanVo
        return vo;
    }

    /**
     * 查询护理计划列表
     *
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan) {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }



    /**
     * 新增护理计划
     * <p>
     * 3.2 将原本的单表保存改造为多表关联保存：
     * a. 护理计划表
     * b. 护理计划和护理项目关联表
     *
     * @return 结果
     */
    @Override
    @Transactional
    public int insertNursingPlan(NursingPlanDto nursingPlanDto) {

        /*// 1. 属性拷贝dto到NursingPlan对象
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto, nursingPlan); // 可以打断点看下这个属性拷贝的效果

        // 2. 保存护理计划到 NursingPlan表
        boolean save = save(nursingPlan);
        if (save) {
            *//*清除缓存*//*
            redisTemplate.delete(CACHE_NURSINGPLAN_KEY);
        }

        // 3. 批量保存护理项目计划关系 到 nursingProjectPlan表: 注意需要保存 plan_id
        return nursingProjectPlanMapper.batchSave(nursingPlanDto.getProjectPlans(), nursingPlan.getId());

        // 4. 涉及多个表的插入，需要事务控制。*/
        // TODO 使用Mybatisplus 第一步拷贝属性
        NursingPlan nursingPlan=new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto,nursingPlan);
        // TODO 使用Mybatisplus 去添加护理计划
        boolean save = save(nursingPlan);
        if(save){
            //添加成功的同时我们去删除缓存
             //*清除缓存*//*
            redisTemplate.delete(CACHE_NURSINGPLAN_KEY);
        }
        // TODO 使用Mybatisplus的方法去插入,会自动有Id不需要跟之前一样拿注解获得id
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
        for (NursingProjectPlan projectPlan : projectPlans) {
            projectPlan.setPlanId(nursingPlan.getId());
        }
        boolean b = nursingProjectPlanService.saveBatch(projectPlans);
        return b?1:0;

    }


    /**
     * 修改护理计划:
     * <p>
     * 3.2 修改护理计划：多表关联修改
     *
     * @return 结果
     */
    @Override
    @Transactional
    public int updateNursingPlan(NursingPlanDto dto) {
        //1.属性拷贝：dto拷贝到nursingPlan中
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(dto, nursingPlan);

        //2.判断dto中的项目列表是否为空,如果不为空，则先删除护理计划与护理项目的关系，然后重新批量添加
        if (dto.getProjectPlans() != null && dto.getProjectPlans().size() > 0) {
            //2.1 删除护理计划对应的护理项目列表
            nursingProjectPlanMapper.deleteByPlanId(nursingPlan.getId());
            //2.2 批量保存护理计划对应的护理项目列表
            nursingProjectPlanMapper.batchSave(dto.getProjectPlans(), dto.getId());
        }

        //3.修改护理计划
        int result= updateById(nursingPlan) ? 1 : 0;
        if (result==1) {
            /*清除缓存*/
            redisTemplate.delete(CACHE_NURSINGPLAN_KEY);
        }
        return result;


        //4.注意事务控制
    }

    /**
     * 批量删除护理计划
     *
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanByIds(Integer[] ids) {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }







    /**
     * 删除护理计划信息
     *
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteNursingPlanById(Integer id) {

        // 1. 删除关联表数据
        nursingProjectPlanMapper.deleteByPlanId(id);

        // 2. 删除计划表数据
        int result=   removeById(id) ? 1 : 0;
        if (result==1) {
            /*清除缓存*/
            redisTemplate.delete(CACHE_NURSINGPLAN_KEY);
        }
        return result;

        // 3. 注意事务控制
    }

    @Override
    public List<NursingPlan> listAll() {
        /*首先我们从Redis里面查缓存*/
        List<NursingPlan> list = (List<NursingPlan>) redisTemplate.opsForValue().get(CACHE_NURSINGPLAN_KEY);
        if (ObjectUtils.isNotEmpty(list)) {
            /*不为空就返回数据*/

            return list;
        }
        /*为空的话就去数据库里面查并且放到缓存里面*/

        LambdaQueryWrapper<NursingPlan> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(NursingPlan::getStatus, 1);
        list = list(queryWrapper);
        redisTemplate.opsForValue().set(CACHE_NURSINGPLAN_KEY, list);
        return list;

    }


}